import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import playlist.Composer;
import config.ConfigFile;
import copy.FileCopy;
import copy.ResourceCopy;

public class PlaylistMain {

	// Files, regrouped by types
	private Map<String, List<File>> mp3s;
	// Ordered files of playlist
	private List<File> playlist;
	// Playlist type
	private String playlistType;
	private File outputDir;
	private File tempPauseFile = null;

	private PlaylistMain() {
		mp3s = new HashMap<String, List<File>>();
		playlist = new ArrayList<File>();
	}

	public static void main(String[] args) throws Throwable {

		System.out.println("\nCa commence...\n");
		ConfigFile.init(args[0]);

		new PlaylistMain().run();
		// Date runLength = new Date(Calendar.getInstance().getTimeInMillis()
		// - start);
		// System.out.println("Duree d'execution : " + runLength.getMinutes()
		// + " minutes et " + runLength.getSeconds() + " secondes.");
		System.out.println("\nC'est fini!\n");
	}

	private void run() throws Throwable {
		String[] playlistTypes = ConfigFile.getProperty("playlistTypes").split(
				",");
		println(playlistTypes);
		System.out.println();

		makeMp3List();

		for (int i = 0; i < playlistTypes.length; i++) {
			playlistType = playlistTypes[i];
			System.out.println("Playlist type : " + playlistType);
			composePlaylist();
			outputPlaylist();
		}
		File outputDir = new File(ConfigFile.getProperty("outputDir"));
		Runtime.getRuntime().exec("explorer " + outputDir.getAbsolutePath());
	}

	private void makeMp3List() {
		File inputDir = new File(ConfigFile.getProperty("inputDir"));
		List<File> files = new ArrayList<File>();
		files.add(inputDir);

		while (files.size() > 0) {
			File current = files.get(files.size() - 1);
			files.remove(files.size() - 1);

			if (current.isDirectory()) {
				File[] sonsList = current.listFiles();

				for (int i = 0; i < sonsList.length; i++) {
					files.add(sonsList[i]);
				}
			} else {
				if (current.getName().toLowerCase().endsWith(".mp3")) {
					storeMp3(current);
				}
			}
		}

		// Cursor1
	}

	private void println(String[] playlistTypes) {
		System.out.println(playlistTypes.length + " playlists :");
		System.out.print('\t');
		for (int i = 0; i < playlistTypes.length; i++) {
			System.out.print(playlistTypes[i]);
			if (i < playlistTypes.length - 1) {
				System.out.print(", ");
			}
		}
		System.out.println();
	}

	private void composePlaylist() throws Throwable {
		Composer composer = null;
		composer = new Composer(mp3s);
		playlist = composer.compose(playlistType);
	}

	private void outputPlaylist() throws Throwable {
		outputDir = generateOutputDir();
		outputDir.mkdirs();
		System.out.print("\t");

		ResourceCopy.copy("/Beep.mp3", new File(outputDir, "00_" + "Beep.mp3"));

		for (int i = 0; i < playlist.size(); i++) {
			File mp3 = playlist.get(i);
			StringBuffer buffer = new StringBuffer();
			if (2 * i < 10) {
				buffer.append('0');
			}
			String outputMp3Name = buffer.append(2 * i + 1).append('_')
					.append(mp3.getName()).toString();

			File outputMp3 = new File(outputDir, outputMp3Name);
			FileCopy.copy(mp3.getAbsolutePath(), outputMp3.getAbsolutePath());
			System.out.print("||");
			createPauseFile(2 * i + 2);
		}

		ResourceCopy.copy("/Beep.mp3", new File(outputDir,
				(2 * playlist.size()) + 1 + "_" + "Beep.mp3"));
		System.out.println("\t100 %");
	}

	private void createPauseFile(int index) throws Throwable {

		if (tempPauseFile == null) {
			tempPauseFile = new File("C:/Temp", "tempPauseFile.mp3");
			ResourceCopy.copy("/Silence25s.mp3", tempPauseFile);
		}

		String prefix = "";
		if (index < 10) {
			prefix = "0";
		}
		File outputFile = new File(outputDir, prefix + index + "_Pause.mp3");
		FileCopy.copy(tempPauseFile, outputFile);
	}

	private File generateOutputDir() {
		int i = 0;
		File outputDir = new File(ConfigFile.getProperty("outputDir"),
				playlistType + '_' + i++);
		while (outputDir.exists()) {
			outputDir = new File(ConfigFile.getProperty("outputDir"),
					playlistType + '_' + i++);
		}
		return outputDir;
	}

	private void storeMp3(File mp3) {
		String parentName = mp3.getParentFile().getName();
		if (!mp3s.containsKey(parentName)) {
			mp3s.put(parentName, new ArrayList<File>());
		}
		mp3s.get(parentName).add(mp3);
	}
}
